package com.ecoolex.hotel.saas.platform.graphql;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.annotation.Description;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.annotation.PlatformAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.auth.platform.PlatformMemberAuthority;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.hotel.saas.entity.Department;
import com.ecoolex.hotel.saas.entity.Distribution;
import com.ecoolex.hotel.saas.platform.core.PlatformConstant;
import com.ecoolex.hotel.saas.pojo.AddDistributionDTO;
import com.ecoolex.hotel.saas.pojo.EditDistributionDTO;
import com.ecoolex.hotel.saas.ws.OperationService;

@GraphQL2Module(type = Type.Platform, description = "分销")
@PlatformAuthoritativeRegister(parentKey = PlatformConstant.AUTH_PROJECT_KEY,
	key = DistributionGraphql.AUTH,
	name = "分销管理",
	remark = "分销管理")
public class DistributionGraphql {

	public static final String AUTH = PlatformConstant.AUTH_PROJECT_KEY + "-DISTRIBUTION";
	
	private static final String AUTH_ADD = AUTH + "-ADD";
	
	private static final String AUTH_EDIT = AUTH + "-EDIT";
	
	private static final String AUTH_GET = AUTH + "-GET";
	
	private static final String AUTH_QUERY = AUTH + "-QUERY";
	
	@Reference
	private OperationService operationService;
	
	@TypeField(type = Department.class, field = "distribution", description = "分销详情")
	public Distribution distribution(@Source Department source, @NonNull @Description({"1-比例分销；2-金额分销"}) Integer distributionType) {
		return operationService.getByDepartmentIdAndType(source.getDepartmentId(), distributionType);
	}
	
//	@QueryField(field = "distribution", description = { "分销详情" })
//	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "获取分销详情")
//	public Distribution get(@NonNull Integer distributionId, PlatformMemberAuthority user) {
//		user.assertAuth(AUTH_GET);
//		return null;
//	}
	
	@MutationField(field = "addDistribution", description = "添加分销")
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加分销")
	public Distribution add(@NonNull AddDistributionDTO add, PlatformMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		return operationService.addDistribution(add, user.userName());
	}
	
	@MutationField(field = "editDistribution", description = "编辑分销")
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑分销")
	public Distribution edit(@NonNull EditDistributionDTO edit, PlatformMemberAuthority user) {
		user.assertAuth(AUTH_EDIT);
		Distribution old = operationService.getDistribution(edit.getDistributionId());
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "分销配置不存在");
		BeanUtil.copy(edit, old);
		return operationService.editDistribution(old, user.userName());
	}
	
}
